#include #include #include #include using std::string; using std::cout; using std::cin; using std::endl; struct Verse { int chapterNumber; int verseNumber; string text; }; struct Book { string title; Verse* verses; //verses is a pointer to an array to be created later int verseCount; Book() //constructor - initialization function { title = ""; verseCount = 0; verses = NULL; } void display(); }; void Book::display() { //cout << title << endl; int chapterNumber = 0; for(int i = 0; i < verseCount; i++) { if(chapterNumber != verses[i].chapterNumber) { cout << endl << title << " Chapter " << verses[i].chapterNumber << endl; chapterNumber++; } cout << verses[i].verseNumber << " "; cout << verses[i].text << " "; } } struct Bible { Book books[66]; Bible(); void readFromFile(); void display(); }; void Bible::display() { for(int i = 0; i < 66; i++) { books[i].display(); } } Bible::Bible() { readFromFile(); } void Bible::readFromFile() { std::ifstream fin("bible.txt"); int bookIndex = -1; //get book names and verse counts while(!fin.eof()) { string line; getline(fin,line); if(line.length() > 0 && line[0] == 'B') { //new book bookIndex++; books[bookIndex].title = line.substr(8); } if(line.length() > 0 && isdigit(line[0])) { //new verse books[bookIndex].verseCount++; } } fin.close(); //dynamically allocate enough memory for each books verses for(int i = 0; i < 66; i++) { books[i].verses = new Verse[books[i].verseCount]; } fin.open("bible.txt"); bookIndex = -1; int verseIndex = 0; //second reading of the bible, read the text while(!fin.eof()) { string line; getline(fin,line); if(line.length() > 0 && line[0] == 'B') { //new book bookIndex++; verseIndex = 0; } if(line.length() > 0 && isdigit(line[0])) { //new verse string verse = line.substr(8); std::istringstream iss(line); while(line.length() > 0 && !fin.eof()) { getline(fin,line); if(line.length() > 0) { verse = verse + " " + line.substr(8); } } char c; iss >> books[bookIndex].verses[verseIndex].chapterNumber; iss>> c; iss >> books[bookIndex].verses[verseIndex].verseNumber; books[bookIndex].verses[verseIndex].text = verse; verseIndex ++; } } fin.close(); } void main() { Bible bible; bible.display(); //bible.displayVersesWith("firmament"); int x = 9; }